import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Button } from 'antd';
import styles from './../Users.css';
import Factory from '../../factory';
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as accountService from '../../../services/AccountDivi';
const daoName = "Compzxqyzhsymail";
const ButtonGroup = Button.Group;
const columns = [
    { title: '员工编号', width: 100, sorter: true, key: 'EMNO', dataIndex: "EMNO", dispaly: true, canEdit: true, filter: true },
    { title: '员工姓名', width: 120, sorter: true, key: 'EMNM', dataIndex: 'EMNM', dispaly: true, canEdit: true, export: true },
    { title: '所属部门', width: 150, sorter: true, key: 'UTNM', dataIndex: "UTNM", dispaly: true, canEdit: true, export: true },
    { title: '邮箱地址', width: 200, sorter: true, key: 'EMAIL', dataIndex: "EMAIL", dispaly: true, canEdit: true, export: true },
];
let controls = [
    { name: 'EMNO', required: true, type: 'text', label: '员工编号', grid: 12 / 24 },
    { name: 'EMNM', required: true, type: 'text', label: '员工姓名', grid: 12 / 24 },
    { name: 'UTNM', required: true, type: 'text', label: '所属部门', grid: 12 / 24 },
    { name: 'EMAIL', required: true, type: 'text', label: '邮箱地址', grid: 12 / 24 },
];
const DataTable = dataTableCreate(daoName);
const EditFrom = dataEditCreate(daoName);
class CompClieZxqyZhsyMailForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false,
            searchData: {},
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20 }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 20 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    searchHandle(data) {
        this.state.searchData = data
    };
    Add(){
        let emid = this.state.searchData.ID;                    //员工ID
        accountService.AddZxqyUserEmail({ emid }
        ).then(function (data) {
            if (data === 'OK') {
                message.success('添加成功')
                this.props.dispatch({
                    type: `${daoName}/fetch`,
                    payload: { daoName }
                });
                this.props.dispatch({
                    type: `${daoName}/setSelectedRowKeys`,
                    payload: { selectedRowKeys: [] }
                });
                const form = this.refs.findForm.getForm();
                form.resetFields();
            } else{
                message.error('添加失败！' + data)
                return;
            }
        }.bind(this)).catch(function (ex) {
            console.log('请求出错', ex)
        });
    }
    del(){
        const key = this.props.selectedRowKeys;
        let emid=key[0];
        accountService.DelZxqyUserEmail({ emid }
        ).then(function (data) {
            if (data === 'OK') {
                message.success('删除成功')
                this.props.dispatch({
                    type: `${daoName}/fetch`,
                    payload: { daoName }
                });
                this.props.dispatch({
                    type: `${daoName}/setSelectedRowKeys`,
                    payload: { selectedRowKeys: [] }
                });
                const form = this.refs.findForm.getForm();
                form.resetFields();
            } else{
                message.error('添加失败！' + data)
                return;
            }
        }.bind(this)).catch(function (ex) {
            console.log('请求出错', ex)
        });
    }
    sendMail(){
        Modal.confirm({
            title: '提示',
            content: `你确认要给下列表员工发送中小综合收益情况邮件吗？`,
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                accountService.sendZxqyMail({ }
                ).then(function (data) {
                    if (data === 'OK') {
                        message.success('邮件发送成功')
                        this.props.dispatch({
                            type: `${daoName}/fetch`,
                            payload: { daoName }
                        });
                    } else{
                        message.error('邮件发送失败！' + data)
                        return;
                    }
                }.bind(this)).catch(function (ex) {
                    console.log('请求出错', ex)
                });
                this.props.dispatch({
                    type: `${daoName}/setSelectedRowKeys`,
                    payload: { selectedRowKeys: [] }
                });
            }
        });
    }
    render() {
        let {permID} = this.props
        const controlsFilter = [
            { name: 'YG', required: false, type: 'search', label: '员工', grid: 5 / 24, layout: { labelSpan: 8, wrapperSpan: 15 }, fetch: { daoName: "UserSel", searchColumns: ['No', 'Name'], optionTpl: 'Name', valueTpl: "ID", onSelect: this.searchHandle.bind(this), permID: permID } },
            { name: 'BT', required: false, type: 'button-group', data: [{ value: '1', label: '添加' , onClick: this.Add.bind(this)}, { value: '2', label: '删除', onClick: this.del.bind(this) }, { value: '3', label: '发送邮件' }], grid: 9 / 24 },
        ];
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} selectFirst={false} clearItemMargin={true} />
                    </div>
                    <DataTable ref="WBTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                </div>
            </div>
        )
    }
}
function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record
    }
}
CompClieZxqyZhsyMailForm = connect(mapStateToProps)(CompClieZxqyZhsyMailForm)
export default CompClieZxqyZhsyMailForm;

let render = (props) => React.createElement(CompClieZxqyZhsyMailForm, props);
let f = new Factory()
f.set("CompClieZxqyZhsyMailForm", render)
